﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;
using Gift.Model;
using Gift.Model.ENUM;
using Gift.DAL;
using System.Data;

namespace WebSite.admin
{
    public partial class index_main :BaseAdmin
    {

        readonly adminlogin theAdminLogin = new adminlogin();
        readonly UserRuleRef theUserRuleRef = new UserRuleRef();
        readonly dalCompany theCompany = new dalCompany();
        readonly DALRule theRule = new DALRule();

        string _viewInMoney = "none";

        public string ViewInMoney
        {
            get { return _viewInMoney; }
            set { _viewInMoney = value; }
        }

         StringBuilder sbInMoneyList = new StringBuilder();

        public string InMoneyList
        {
            get
            {
                return sbInMoneyList.ToString();
            }
        }

        string sInMoneyFormat =" <td width=\"30%\" align=\"right\"><a href=\"company/OutInDetail.aspx?id={0}\">{1}</a></td>"
                                     + " <td width=\"20%\"><strong>{2}</strong>笔预存款</td> ";

        // Methods
        private void Bind()
        {
            string sWhere = string.Empty;
            if (base.CompanyLevelId == "1")
            {
                sWhere =  " and OrderCode in (select oa.orderCode from tbOrderAccount oa join DB_Card c on oa.cardId = c.CardId and c.CompanyId = " + base.CompanyId + ")";

            }
            this.Label1.Text = count.counts("", "DB_order", "states", "0" + sWhere);
            this.Label2.Text = count.counts("", "DB_order", "states", "1" + sWhere);
            this.Label3.Text = count.counts("", "DB_order", "states", "2" + sWhere);
            this.Label4.Text = count.counts("", "DB_order", "states", "3" + sWhere);
         //   this.Label5.Text = count.counts("", "DB_order", "states", "3");
            this.goods1.Text = count.countsb("", "DB_Shoppro", "visible", "1", "protype", "0");
            this.goods2.Text = count.countsb("", "DB_Shoppro", "visible", "1", "protype", "1");
            this.goods3.Text = count.counts("", "DB_Shoppro", "proisnew1", "1");
            this.goods4.Text = count.counts("", "DB_Shoppro", "proisnew2", "1");
            this.goods5.Text = count.counts("", "DB_Shoppro", "proisnew3", "1");
            this.goods6.Text = count.counts("", "DB_Shoppro", "iscuxao", "1");
            this.msg.Text = count.counts("", "DB_Msg", "1", "1");
            this.comment.Text = count.counts("", "DB_Comment", "1", "1");
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            if(Session["calluser"] != null)
            {
                Response.Redirect("order/OrderAdd.aspx");
            }
             if (!base.IsPostBack)
            { 
                this.ValidateLoginRule();
                if (Request.QueryString["uid"] == AdminUserId)
                {
                    Response.Write("<script t language=javascript>window.location.href='user/adduser.aspx?uid=" + AdminUserId + "';</script>");
                }
                if (base.GetValidateLoginRule(ERule.CompanyMoneyList))
                {
                    _viewInMoney = "block";
                    this.BindInMoney();
                }

                if (base.GetValidateLoginRule(ERule.OpenCardManager))
                {

                        this.tblOpenCard.Visible = true;
                        this.lblApplicationCard.Text = this.BindApplicationCard("6");
       
                        this.lblPass.Text = this.BindApplicationCard("1");
                    
                }
            }
        }


        void ValidateLoginRule()
        {
            int iAdminId = theAdminLogin.GetIdByName(this.Session["Adminlogin"].ToString());
            tbCompany oCompany = theCompany.GetModelByAdminId(iAdminId);

            DataSet ds;
            ds = theUserRuleRef.GetListByAdminId(iAdminId);
            if (ds != null && ds.Tables.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    ERule oERule = (ERule)Enum.Parse(typeof(ERule), dr["Name"].ToString(), true);
                    if (oERule == ERule.OrderProcess)
                    {
                        
                        this.tbOrder.Visible = true;
                        this.Bind();
                        return;
                    } 
                }
            }
        }

        string BindApplicationCard(string sStates)
        {
 

            string sqlCount = " select count(1)  ";
            string sqlBefor = "select ROW_NUMBER() OVER (ORDER BY cardcode asc, starttime desc, cardid asc) AS RowNumber,*,(select [name] from tbCardState where Id = db_card.states) as statesName,convert(varchar(10),starttime,121) + ' 至 ' + convert(varchar(10),endtime,121) as exptime, (select Name from tbCardType where Id = DB_Card.CardTypeId) as CardTypeName"
                          + " ,(select CompanyShortName from tbCompany where Id=DB_Card.companyId  ) + '->' + isnull((select CompanyShortName from tbCompany where Id=DB_Card.IICompanyId  ),' ') + '->' + isnull((select CompanyShortName from tbCompany where Id=DB_Card.IIICompanyId  ),' ') + '->' + isnull((select CompanyShortName from tbCompany where Id=DB_Card.IVCompanyId  ),' ') as companyName"
                           + ", case (select top 1 1 from tbGMS where cardid=DB_Card.cardid) when 1 then '已发' else '未发' end as isSend ";
            string sql = " from DB_Card where 1=1  ";

            if (sStates == "1")
            {
                sql = sql + " and states in (1,5)";
            }
            else if (sStates == "6")
            {
                sql = sql + " and states = 6 ";
            }
   

            if (!string.IsNullOrEmpty( base.CompanyId ) )
            {
                sql = sql + string.Format(" and (companyId={0} or IICompanyId={1} or IIICompanyId={2} or IVCompanyId={3})"
                                           , base.CompanyId
                                           , base.CompanyId
                                           , base.CompanyId
                                           , base.CompanyId);
            }

            sqlCount = sqlCount + sql;
            sql = sqlBefor + sql;

            DataTable dtRowCount = SQLAccess.GreatDs(sqlCount).Tables[0];
             return dtRowCount.Rows[0][0].ToString();
        }
        void BindInMoney()
        {
            DataSet ds = theCompany.GetNewInMoneyList(DateTime.Now.AddDays(-3).ToString("yyyy-MM-dd"), DateTime.Now.ToString("yyyy-MM-dd"));
            if (ds != null)
            {
                DataTable dt = ds.Tables[0];
                sbInMoneyList.Append("<tr>");
                int i = 0;
                foreach (DataRow dr in dt.Rows)
                {
                    if (base.CompanyId == null || base.CompanyId == dr["ParentId"].ToString())
                    {
                        i++;
                        sbInMoneyList.AppendFormat(sInMoneyFormat
                                                   , dr["CompanyId"].ToString()
                                                   , dr["CompanyName"].ToString()
                                                   , dr["InMoneyCount"].ToString()
                                                   );
                        if (i % 2 == 0)
                        {
                            sbInMoneyList.Append("<tr/>");
                        }
                    }
                }

            }
        }


    }
}